package jsu.blogger.mapper;

import jsu.blogger.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface CommentMapper {
    @Select("select comment_id as 'commentId'" +
            ",user_id as 'userId'" +
            ",post_id as 'postId'" +
            ",parent_comment_id as 'parentCommentId'" +
            ",reply_to_user_id as 'replyToUserId'" +
            ",content" +
            ",created_at as 'createdAt'" +
            ",likes_count as'likesCount'" +
            ",dislikes_count as 'dislikesCount'" +
            ",replies_count as 'repliesCount'" +
            "from comments where  parent_comment_id is null and post_id=#{postId} limit #{begin},#{count}")
    public List<Comment> selectParentCommentByPostId(@Param("postId") Long postId,@Param("begin") int begin,@Param("count") int count);

    @Select("select comment_id as 'commentId'" +
            ",user_id as 'userId'" +
            ",post_id as 'postId'" +
            ",parent_comment_id as 'parentCommentId'" +
            ",reply_to_user_id as 'replyToUserId'" +
            ",content" +
            ",created_at as 'createdAt'" +
            ",likes_count as'likesCount'" +
            ",dislikes_count as 'dislikesCount'" +
            ",replies_count as 'repliesCount'" +
    "from comments where post_id=#{postId} and parent_comment_id=#{parentCommentId} limit #{begin},#{count}")
    public List<Comment> selectSonCommentByPostIdAndParentCommentId(@Param("postId") Long postId,@Param("parentCommentId") Long parentCommentId ,@Param("begin") int begin, @Param("count") int count);

    @Insert("INSERT INTO comments (post_id, user_id, parent_comment_id, reply_to_user_id, content, created_at, likes_count, dislikes_count, replies_count) " +
            "VALUES (#{postId}, #{uid}, #{parentCommentId}, #{replyToUserId}, #{content}, #{createdAt}, 0, 0, 0)")
    public int insertComment(@Param("postId") Long postId,
                      @Param("uid") Long userId,
                      @Param("parentCommentId") Long parentCommentId,
                      @Param("replyToUserId") Long replyToUserId,
                      @Param("content") String content,
                      @Param("createdAt") Timestamp createdAt);
    @Delete("DELETE FROM comments WHERE comment_id = #{commentId} AND user_id = #{uid}")
    public int deleteComment(@Param("commentId") Long commentId, @Param("uid") Long userId);
    @Delete("DELETE FROM comments WHERE parent_comment_id = #{commentId}")
    public int deleteSonComment(@Param("commentId") Long commentId);
    @Update("update comments set replies_count=replies_count+1 where comment_id=#{commentId}")
    public int updateIncreaseParentCommentRepliesCount(@Param("commentId")Long commentId);
    @Update("update comments set replies_count=replies_count-1 where replies_count>0 and comment_id=#{commentId}")
    public int updateDecreaseParentCommentRepliesCount(@Param("commentId")Long commentId);
    @Select("select parent_comment_id from comments where comment_id=#{commentId}")
    public Long selectIsSonCommentByCommentId(@Param("commentId") Long commentId);
    @Select("select content from comments where comment_id=#{commentId}")
    public String selectParentCommentByCommentId(@Param("commentId") Long commentId);
}
